package com.example.study.plus.mybatis_plus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.study.config.JsonResult;
import com.example.study.plus.mybatis_plus.entity.ChatMessages;
import com.example.study.plus.mybatis_plus.entity.User;
import com.example.study.plus.mybatis_plus.service.ChatMessagesService;
import com.example.study.plus.mybatis_plus.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 聊天信息表 前端控制器
 * </p>
 *
 * @author 路飞4
 * @since 2023-08-29
 */
@RestController
@CrossOrigin//同源问题
@RequestMapping("/study")
@Slf4j
public class ChatMessagesController {
    @Resource
    UserService userService;
    @Resource
    ChatMessagesService chatMessagesService;

    @ApiOperation("发送消息")
    @PostMapping("/sendAMessage")
    public JsonResult<String> sendAMessage(@ApiParam("发送者") @RequestParam("sender_id") int sender_id, @ApiParam("接收者") @RequestParam("receiver_id") int receiver_id, @ApiParam("消息") @RequestParam("message") String message, @ApiParam("是否推送") @RequestParam("pushed") boolean pushed) {
        if (sender_id==receiver_id){
            return new JsonResult<>(101,"失败");
        }
        int i = chatMessagesService.sendAMessage(sender_id, receiver_id, message,pushed);

        return new JsonResult<>(i+"");
    }

    @ApiOperation("查询消息列表")
    @PostMapping("/queryTheListOfMessages")
    public JsonResult<List<ChatMessages>> queryTheListOfMessages(@ApiParam("发送者") @RequestParam("sender_id") int sender_id, @ApiParam("接收者") @RequestParam("receiver_id") int receiver_id, @ApiParam("页数") @RequestParam("page") int page, @ApiParam("长度") @RequestParam("pageSize") int pageSize){
        Page<ChatMessages> chatMessages = chatMessagesService.queryTheListOfMessages(sender_id, receiver_id,page,pageSize);

        return new JsonResult<>(chatMessages.getRecords());
    }
}
